package com.zoe.app.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @author Zoe.Zhang
 * @date 2021/11/10
 * @description
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    private static final Logger logger = LoggerFactory.getLogger(RedisKeyExpirationListener.class);


    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }


    /**
     * 针对redis数据失效事件，进行数据处理,获取不到 失效key对应的值,所以如果所需要的信息如果不多可以尝试放到key上,如果比较多还是老老实实查询吧
     *
     * @param message
     * @param pattern
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        try {
            String key = message.toString();
            //从失效key中筛选代表订单失效的key
            // ORDER_1458386280713875456
            System.out.println("失效key+" + key);
        }
        catch (Exception e) {
            e.printStackTrace();
            logger.error("【修改支付订单过期状态异常】：" + e.getMessage());
        }
    }
}
 